package com.bluefish.bltuan.action.admin;

import org.apache.struts2.rest.DefaultHttpHeaders;
import org.apache.struts2.rest.HttpHeaders;

import com.bluefish.bltuan.annotation.Before;
import com.bluefish.bltuan.module.mail.EmailPurpose;
import com.bluefish.bltuan.module.mail.EmailReceiver;
import com.bluefish.bltuan.module.mail.EmailTemplate;
import com.bluefish.bltuan.service.EmailTemplateService;

public class EmailTemplateAction extends AdminAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = -417456948587228631L;

	private EmailTemplate template;
	
	private EmailTemplateService emailTemplateService;
	
	private String name;
	
	private EmailPurpose purpose;
	
	private EmailReceiver receiver;
	
	private String title;
	
	private String content;
	
	private EmailPurpose[] purposes = EmailPurpose.values();
	
	private EmailReceiver[] receivers = EmailReceiver.values();
	
	@Before(include={"create", "show"})
	public void prepare(){
		String str = this.getId();
		this.purpose = EmailPurpose.valueOfString(str);
		this.template = this.emailTemplateService.findByPurpose(this.purpose);
		if(this.template == null)
			this.template = new EmailTemplate();
	}
	
	@Override
	public HttpHeaders create() {
		setProperties();
		this.emailTemplateService.saveOrUpdate(this.template);
		redirect();
		return null;
	}

	@Override
	public String destroy() {
		return null;
	}

	@Override
	public String edit() {
		return null;
	}

	@Override
	public HttpHeaders editNew() {
		return null;
	}

	@Override
	public HttpHeaders index() {
		return null;
	}

	@Override
	public HttpHeaders show() {
		return new DefaultHttpHeaders("show").disableCaching();
	}

	@Override
	public String update() {
		return null;
	}

	private void setProperties() {
		this.template.setName(this.name);
		this.template.setTitle(this.title);
		this.template.setContent(this.content);
		this.template.setPurpose(this.purpose);
		this.template.setReceiver(this.receiver);
	}

	public void setEmailTemplateService(
			EmailTemplateService emailTemplateService) {
		this.emailTemplateService = emailTemplateService;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public void setTitle(String title) {
		this.title = title;
	}
	
	public void setContent(String content) {
		this.content = content;
	}
	
	public void setPurpose(String purpose) {
		this.purpose = EmailPurpose.valueOfString(purpose);
	}
	
	public void setReceiver(String receiver) {
		this.receiver = EmailReceiver.valueOf(receiver);
	}
	
	public EmailPurpose[] getPurposes() {
		return purposes;
	}
	
	public EmailReceiver[] getReceivers() {
		return receivers;
	}
	
	public EmailPurpose getPurpose() {
		return purpose;
	}
	
	public EmailTemplate getTemplate() {
		return template;
	}
}
